package com.jn.exam.paperConsumer.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jn.exam.HTMLUtil;
import com.jn.exam.paperConsumer.entity.PaperQuestion;
import com.jn.exam.paperConsumer.entity.PaperQuestionAnswer;
import com.jn.exam.paperConsumer.entity.StudentConnection;
import com.jn.exam.paperConsumer.entity.vo.Answer;
import com.jn.exam.paperConsumer.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 江南大学1033190417
 * @date 2022/1/19 18:15
 */
@Component
@EnableBinding(Sink.class)
public class ReceiveMessageListener {

    @Autowired
    private SemanticAnalysisService semanticAnalysisService;

    @Autowired
    private PaperQuestionAnswerService paperQuestionAnswerService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private ExamService examService;

    @StreamListener(Sink.INPUT)
    @Transactional
    public void input(Message<Answer> message) {
        Answer answer = message.getPayload();
        MessageHeaders headers = message.getHeaders();
        String studentId = (String) headers.get("studentId");
        String examId=(String)headers.get("examId");
        String ans = answer.getAnswer();//学生答案
        String paperQuestionId = answer.getPaperQuestionId();
        PaperQuestion paperQuestion = (PaperQuestion) questionService.getPaperQuestionById(paperQuestionId).getData().get("paperQuestion");
        Float score;
        String trueAnswer = paperQuestion.getAnswer();//正确答案
        //只能匹配
        if (trueAnswer.equals(ans)) {
            score = paperQuestion.getScore();
        } else {
            String t1 = HTMLUtil.getHtmlText(trueAnswer);
            String t2 = HTMLUtil.getHtmlText(ans);
            Double aDouble = semanticAnalysisService.SemanticAnalysis(t1, t2);
            score = aDouble.floatValue() * paperQuestion.getScore();
        }
        PaperQuestionAnswer paperQuestionAnswer = new PaperQuestionAnswer();
        paperQuestionAnswer.setAnswer(ans).setPaperQuestionId(paperQuestionId).setStudentId(studentId).setScore(score).setIsMarked(false);
        paperQuestionAnswerService.saveOrUpdateAnswer(paperQuestionAnswer);
        //更新学生考试进度
        StudentConnection studentConnection=new StudentConnection();
        studentConnection.setProgress(answer.getProgress()).setExamId(examId).setStudentId(studentId);
        examService.updateExamStudentConnectionForFeign(studentConnection);
    }
}



